<?php
include('../../includes/classes/core.php');
$action 	= $_REQUEST['act'];
$user_id	= $_SESSION['USERID'];
$error		= '';
$output 	= '';

switch ($action) {
	case 'get_add_page':
		$page		= GetPage();
		$output 	= array('page'	=> $page);
		
		break;
	case 'get_edit_page':
		$catridge_part_id		= $_REQUEST['id'];
		$page					= GetPage(GetCartridgePart($catridge_part_id));
		$output 				= array('page'	=> $page);
		
		break;
	case 'get_list':
		$count 		= $_REQUEST['count'];
		$hidden		= $_REQUEST['hidden'];
		$rResult	= mysql_query("SELECT	`cartridge_parts_type`.`id`,
											`cartridge_parts_type`.`name`,
											`cartridge_parts_category`.`name`,
									CASE 
										WHEN `cartridge_parts_type`.`default` = 0 THEN 'არა'
						  				WHEN `cartridge_parts_type`.`default` = 1 THEN 'კი'
									END as defaulta,
									CASE 
										WHEN `cartridge_parts_type`.`checked` = 0 THEN 'არა'
						  				WHEN `cartridge_parts_type`.`checked` = 1 THEN 'კი'
									END as checkeda
									FROM	`cartridge_parts_type`
									LEFT JOIN	`cartridge_parts_category` ON `cartridge_parts_category`.`id` = `cartridge_parts_type`.`category`
									WHERE   `cartridge_parts_type`.`actived` = 1");
		
		$output = array(
				"aaData"	=> array()
		);
			
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$output['aaData'][] = $row;
		}

		break;
	case 'save_cartridge_part':
		$catridge_part_id		= $_REQUEST['id'];
		$name 		    		= $_REQUEST['na'];
		$cycle					= $_REQUEST['cycle'];
		$default 		    	= $_REQUEST['d'];
		$checked 		    	= $_REQUEST['c'];
		$category		    	= $_REQUEST['cat'];

		if($catridge_part_id == ''){
			AddObject($user_id, $name, $cycle, $default, $checked, $category);
		}else{
			SaveObject($user_id,$catridge_part_id,$name, $cycle, $default, $checked, $category);
		}
		
		break;
	case 'disable':
		$catridge_part_id = $_REQUEST['id'];
		Disable($catridge_part_id);
		
		break;
	default:
		$error = 'Action is Null';
}


$output['error'] = $error;
echo json_encode($output);

function AddObject($user_id, $name, $cycle, $default,$checked,$category)
{
		mysql_query("INSERT INTO `cartridge_parts_type`
						(`user_id`,`name`, `cycle`, `default`,`checked`,`category`)
					VALUES
						($user_id, '$name', $cycle, $default,$checked,$category)");
}

function SaveObject($user_id, $catridge_part_id, $name, $cycle, $default, $checked, $category)
{
		mysql_query("UPDATE
						`cartridge_parts_type`
					SET
						`user_id`	= $user_id,
						`name`		= '$name',
						`cycle`		= $cycle,	
						`default`	= $default,
						`checked`	= $checked,
						`category`	= $category
					WHERE
						`id`		= '$catridge_part_id'");
}

function Disable($catridge_part_id)
{
	mysql_query("UPDATE `cartridge_parts_type`
				SET    `actived` = 0
				WHERE 	`id` = '$catridge_part_id'");
}

function GetpartDefault($catridge_part_id){
	$data = '';
	$req = mysql_query("SELECT	`cartridge_parts_type`.`default`
						FROM	`cartridge_parts_type`
						WHERE   `cartridge_parts_type`.`actived` = 1 &&  `cartridge_parts_type`.`id`= '$catridge_part_id'");
	$req = mysql_fetch_array( $req );
	if($catridge_part_id == ''){
		$data .= '<option value="2" selected="selected"></option>';
	}else{
		$data .= '<option value="2"></option>';
	}
	
	$array = array('არა','კი');
	for($i=0;$i<2;$i++){
		if( $req['default'] == "$i"){
			$data .= '<option value="' . $i . '" selected="selected">' . $array[$i] . '</option>';
		}else{
			$data .= '<option value="' . $i . '">' . $array[$i] . '</option>';
		}
	}
	return $data;
}

function GetpartChecked($catridge_part_id){
	$data = '';
	$req = mysql_query("SELECT  `cartridge_parts_type`.`checked`
						FROM	`cartridge_parts_type`
						WHERE   `cartridge_parts_type`.`actived` = 1 &&  `cartridge_parts_type`.`id`= '$catridge_part_id'");
	$req = mysql_fetch_array( $req );
	if($catridge_part_id == ''){
		$data .= '<option value="2" selected="selected"></option>';
	}else{
		$data .= '<option value="2"></option>';
	}
	
	$array = array('არა','კი');
	for($i=0;$i<2;$i++){
		if( $req['checked'] == "$i"){
			$data .= '<option value=" '. $i .'" selected="selected"> '.$array[$i].'  </option>';
		}else{
			$data .= '<option value=" '.$i .' "> '.$array[$i].'  </option>';
		}
	}		
	return $data;		
}

function GetCartridgePart($catridge_part_id)
{
	$res = mysql_fetch_assoc(mysql_query("	SELECT	`cartridge_parts_type`.`id`,
													`cartridge_parts_type`.`name`,
													`cartridge_parts_type`.`cycle`,			
													`cartridge_parts_type`.`default`,
													`cartridge_parts_type`.`checked`
											FROM	`cartridge_parts_type`
											WHERE   `cartridge_parts_type`.`actived` = 1 && `id` = '$catridge_part_id'"));
	return $res;
}

function GetpartCategory($catridge_part_id){
	$data = '';
	$req = mysql_query("SELECT  `cartridge_parts_type`.`category`
						FROM	`cartridge_parts_type`
						WHERE   `cartridge_parts_type`.`actived` = 1 &&  `cartridge_parts_type`.`id` = '$catridge_part_id'");
	$req = mysql_fetch_array( $req );
	if($catridge_part_id == ''){
		$data .= '<option value="0" selected="selected"></option>';
	}else{
		$data .= '<option value="0"></option>';
	}
	
	$rResult = mysql_query("SELECT		`cartridge_parts_category`.`id`,
										`cartridge_parts_category`.`name`
							FROM 		`cartridge_parts_category`");
	
	while ( $aRow = mysql_fetch_array( $rResult ) )
	{
		if( $req['category'] == $aRow[0] ){
			$data .= '<option value=" '. $aRow[0] .'" selected="selected"> '.$aRow[1].'  </option>';
		}else{
			$data .= '<option value="' .$aRow[0]. '"> '.$aRow[1].' </option>';
		}
	}
	return $data;
}

function GetPage($res = '')
{
	$data = '
			<div id="dialog-form">
			    <fieldset>
			    	<legend>ძირითადი ინფორმაცია</legend>
		
			    	<table class="dialog-form-table">
						<tr>
							<td style="width: 170px;"><label for="cartridge_part_name">სახელი</label></td>
							<td>
								<div class="seoy-row" id="barcode_client_seoy">
									<input type="text" id="cartridge_part_name" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['name'] . '" />
								</div>
							</td>
						</tr>
						<tr>
							<td style="width: 170px;"><label for="cartridge_part_cycle">ციკლი</label></td>
							<td>
								<input type="text" style="width: 100px !important;" id="cartridge_part_cycle" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['cycle'] . '" />
							</td>
						</tr>										
						<tr>
							<td style="width: 170px;"><label for="cartridge_part_category">კატეგორია</label></td>
							<td>
								<select id="cartridge_part_category" class="idls">' . GetpartCategory($res['id']) . '</select>
							</td>
						</tr>												
						<tr>
							<td style="width: 170px;"><label for="cartridge_part_default">ნაგულისხმევი მნიშვნელობა</label></td>
							<td>
								<select id="cartridge_part_default" class="idls">' . GetpartDefault($res['id']) . '</select>
							</td>
						</tr>
						<tr>
							<td style="width: 170px;"><label for="cartridge_part_checked">მონიშვნა</label></td>
							<td>
								<select id="cartridge_part_checked" class="idls">' . GetpartChecked($res['id']) . '</select>
							</td>
						</tr>
					</table>
					<!-- ID -->
					<input type="hidden" id="cartridge_part_id" value="'.$res['id']. '" />
		        </fieldset>
		    </div>
	    ';
	return $data;
}
?>